<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page import="java.util.Map" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网站首页</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px 0;
            margin-bottom: 30px;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .welcome-message {
            font-size: 16px;
            color: #2c3e50;
        }

        .logout-btn {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
            transition: background-color 0.3s;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        .main-content {
            background-color: #ffffff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
        }

        h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 28px;
        }

        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 30px;
        }

        .feature-card {
            background-color: #f8f9fa;
            border-radius: 6px;
            padding: 20px;
            transition: transform 0.3s, box-shadow 0.3s;
        }

        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .feature-card h3 {
            color: #3498db;
            margin-bottom: 10px;
        }

        footer {
            text-align: center;
            margin-top: 50px;
            padding: 20px;
            color: #7f8c8d;
            font-size: 14px;
        }

        .remember-me {
            margin-top: 20px;
            font-size: 14px;
            color: #555;
        }
        .site-stats {
            margin-top: 20px;
            font-size: 12px;
            color: #666;
            border-top: 1px solid #eee;
            padding-top: 10px;
        }
        .user-panel {
            background-color: #e3f2fd;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<header>
    <div class="container header-content">
        <div class="logo">我的网站</div>
        <div class="user-info">
            <%
                // 首先检查session
                String username = (String)session.getAttribute("username");

                // 如果session中没有，检查cookie
                if(username == null) {
                    Cookie[] cookies = request.getCookies();
                    if(cookies != null) {
                        for(Cookie cookie : cookies) {
                            if("rememberMeUser".equals(cookie.getName())) {
                                username = cookie.getValue();
                                // 将cookie中的用户名存入session
                                session.setAttribute("username", username);
                                break;
                            }
                        }
                    }
                }

                if(username != null) {
                    out.print("<span class='welcome-message'>欢迎您，" + username + "！</span>");
                    out.print("<a href='logout.jsp' class='logout-btn'>退出登录</a>");
                } else {
                    response.sendRedirect("login.jsp");
                }
            %>
        </div>
    </div>
</header>

<div class="container">
    <div class="main-content">
        <h2>欢迎访问我们的网站</h2>
        <p>这里是网站的主要内容区域，您可以在这里找到各种有用的信息和功能。</p>

        <div class="remember-me">
            <div class="user-panel">
                <h3>欢迎, <%= session.getAttribute("username") %>!</h3>
                <p>您是第 <%= application.getAttribute("totalVisits") %> 位访问者</p>
                <p>当前有 <%= application.getAttribute("currentUsers") %> 位用户在线</p>
                <p>系统共有 <%= ((Map<String,String>)application.getAttribute("userDatabase")).size() %> 位注册用户</p>
            </div>

            <%
                // 显示记住我状态
                boolean rememberMe = false;
                Cookie[] cookies = request.getCookies();
                if(cookies != null) {
                    for(Cookie cookie : cookies) {
                        if("rememberMe".equals(cookie.getName()) && "true".equals(cookie.getValue())) {
                            rememberMe = true;
                            break;
                        }
                    }
                }
                if(rememberMe) {
                    out.print("<p><i class='fas fa-check-circle'></i> 您已启用\"记住我\"功能，下次访问将自动登录</p>");
                }
            %>
        </div>

        <div class="features">
            <div class="feature-card">
                <h3>最新公告</h3>
                <p>了解我们最新的产品更新和网站动态。</p>
            </div>
            <div class="feature-card">
                <h3>特色服务</h3>
                <p>探索我们提供的各种特色服务和功能。</p>
            </div>
            <div class="feature-card">
                <h3>帮助中心</h3>
                <p>有问题？我们的帮助中心随时为您服务。</p>
            </div>
        </div>
    </div>
</div>
<!-- 显示网站统计信息 -->
<div class="site-stats">
    <div class="site-stats">
        总访问量: <%= application.getAttribute("totalVisits") %> |
        当前在线: <%= application.getAttribute("currentUsers") %> |
        注册用户: <%= ((Map<String,String>)application.getAttribute("userDatabase")).size() %>
    </div>
</div>
<footer>
    <p>© 2023 我的网站. 保留所有权利.</p>
</footer>
</body>
</html>